import { _decorator, Button, Label, Node, tween, Tween } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import { ClientAPI } from '../../api/api.client';
import type { Popup } from '../page/pop-up';
const { ccclass, property } = _decorator;

@ccclass('ppDiamondFruit')
export class ppDiamondFruit extends BasePopUp<typeof Popup, number> {
	protected default_return: null;

	private lbl_diamond: Label;
	private light_head: Node;
	private light_diamond: Node;

	private diamond: number;

	protected initAfterOnLoad(){
		this.init();
		this.initUIStatus();
	}

	public onShow(diamond: number){
		this.diamond = diamond;
		this.lbl_diamond.string = 'x '+diamond;

		[this.light_head, this.light_diamond].forEach(light=>{
			Tween.stopAllByTarget(light);
			tween(light).set({angle: 0})
			.to(5, {angle: -360}).union()
			.repeatForever().start();
		});
	}

	private init(){
		let node_fruit = this.node.getChildByName('fruit');
		this.lbl_diamond = node_fruit.getChildByName('lbl-value').getComponent(Label);
		this.light_diamond = node_fruit.getChildByName('light-diamond');
		this.light_head = this.node.getChildByName('light');
	}

	private initUIStatus(){
		this.node.getChildByName('lbl-head').getComponent(Label).string = 'CONGRATULATIONS!';
		this.node.getChildByName('lbl-video').getComponent(Label).string = 'CLAIM';
		this.__btn_map__.get('reap').getComponent(Label).string = 'CLAIM'
	}

	private btn_lock = false;
	protected _click_event_: { [name: string]: (this: ppDiamondFruit, button: Button) => void; } = {
		reap(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			this.exit(this.diamond);
		},
		video(){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			ClientAPI.AD('incentive')
			.then(success=>this.exit(this.diamond * (success ? 2 : 1)));
		}
	}

	private exit(diamond: number){
		[this.light_head, this.light_diamond].forEach(light=>{
			Tween.stopAllByTarget(light);
			light.angle = 0;
		});

		this.close(diamond);

		this.btn_lock = false;
	}
}

